<template>
  <div>
    <!-- <h1>菜单管理</h1> -->
    <el-table border row-key="id" style="width: 100%;margin-bottom: 20px;" :data="flagListForMenuAndButtonPermission">
      <el-table-column label="名称" align="center" prop="name"></el-table-column>
      <el-table-column label="权限值" align="center" prop="code"></el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime"></el-table-column>
      <el-table-column label="操作" align="center">
        <!-- 注：此处的“row”就是“与当前鼠标光标所在行对应的存储了菜单或按钮权限信息的对象”↓ -->
        <template #default="{ row, $index }">
          <el-button type="primary" size="small" v-if="row.level !== 4"
            @click="handlerForUserClickTheAddMenuOrBtnButton(row)" v-haveThisButton="`btn.Permission.add`">{{ row.level == 3 ? '添加按钮' : '添加菜单' }}</el-button>
          <!-- <el-button type="primary" size="small" :disabled="row.level == 4?true:false" @click="handlerForUserClickTheAddMenuOrBtnButton(row)">{{row.level == 3?'添加按钮':'添加菜单'}}</el-button> -->
          <el-button type="primary" size="small" :disabled="row.level == 1 ? true : false"
            @click="handlerForUserClickTheEditMenuOrBtnButton(row)" v-haveThisButton="`btn.Permission.update`">编辑</el-button>
          <el-popconfirm :title="`你确认要删除${row.name}吗？`" width="260px" @confirm="handlerForUserClickTheConfirmButtonInThePopConfirmComponent(row)">
            <template #reference>
              <el-button type="danger" size="small" :disabled="row.level == 1 ? true : false" v-haveThisButton="`btn.Permission.remove`">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件（被用于添加或修改项目中的菜单和按钮）↓ -->
    <el-dialog v-model="isTheDialogVisible" :title="ParamsForAddSubMenuOrUpdateMenu.id ? '修改菜单或按钮' : '添加菜单或按钮'">
      <el-form label-width="60px">
        <el-form-item label="名称">
          <el-input v-model="ParamsForAddSubMenuOrUpdateMenu.name" placeholder="请您输入菜单/按钮的名称"></el-input>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input v-model="ParamsForAddSubMenuOrUpdateMenu.code" placeholder="请您输入菜单/按钮的权限值"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="isTheDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handlerForUserClickTheConfirmButton">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="Permission">
import { onMounted, ref, reactive } from 'vue'
import type {
  typeOfResponseDataOfTheApiThatUsedToGetFlagOfAllOfMenuAndButtonPermission,
  menuOrButtonPermissionList,
  typeOfObjectThatstoreInformationAboutMenuOrButtonPermission,
  ParamsForAddSubMenuOrUpdateMenu,
} from "@/api/acl/menu/type"
import {
  requestGetFlagOfAllOfMenuAndButtonPermission,
  requestAddSubmenuToAMenuOrUpdateAMenu,
  requestDeleteMenu,
} from "@/api/acl/menu"
import { ElMessage } from 'element-plus'
// 定义一个变量，它被用于存储所有菜单和按钮的权限标识信息↓
let flagListForMenuAndButtonPermission = ref<menuOrButtonPermissionList>([])
let isTheDialogVisible = ref<boolean>(false) // 定义一个变量，用来控制对话框的显示与隐藏
// 定义一个变量，用来存储添加子菜单或更新菜单时所需要传递的参数信息↓
let ParamsForAddSubMenuOrUpdateMenu = reactive<ParamsForAddSubMenuOrUpdateMenu>({
  id: 0,
  name: '',
  code: '',
  pid: 0,
})
onMounted(() => {
  getFlagOfAllOfMenuAndButtonPermission()
})
/**
 * 获取所有菜单和按钮的权限标识信息
 */
async function getFlagOfAllOfMenuAndButtonPermission() {
  let result: typeOfResponseDataOfTheApiThatUsedToGetFlagOfAllOfMenuAndButtonPermission = await requestGetFlagOfAllOfMenuAndButtonPermission()
  console.log(result) // {code: 200, message: '成功', data: Array(1), ok: true}
  if (result.code == 200) {
    flagListForMenuAndButtonPermission.value = result.data
  }
}
/**
 * @description 处理用户点击“被用于添加菜单或按钮”的按钮时发生的事件
 */
function handlerForUserClickTheAddMenuOrBtnButton(row: typeOfObjectThatstoreInformationAboutMenuOrButtonPermission) {
  Object.assign(ParamsForAddSubMenuOrUpdateMenu, {
    id: 0,
    name: '',
    code: '',
    pid: 0,
    level: 0,
  }) // 清空上次操作后残留的数据
  isTheDialogVisible.value = true
  console.log(row) // Proxy(Object) {id: 1, createTime: '2019-11-15 17:13:06', updateTime: '2020-09-25 13:47:54', pid: 0, name: '全部数据', …}
  ParamsForAddSubMenuOrUpdateMenu.level = row.level + 1 // 注：因为新增的菜单是当前菜单的下级菜单，所以需要将当前菜单的level+1后，再赋值给新增菜单的level属性↓
  ParamsForAddSubMenuOrUpdateMenu.pid = (row.id as number) // 把当前菜单的ID作为新增的菜单的父级菜单的ID
}
/**
 * @description 处理用户点击“被用于编辑菜单或按钮”的按钮时发生的事件
 */
function handlerForUserClickTheEditMenuOrBtnButton(row: typeOfObjectThatstoreInformationAboutMenuOrButtonPermission) {
  isTheDialogVisible.value = true
  Object.assign(ParamsForAddSubMenuOrUpdateMenu, row) // 把当前菜单或按钮的权限信息赋值给新增/编辑表单中的数据
}
/**
 * @description 处理用户点击“确定”按钮时发生的事件
 */
async function handlerForUserClickTheConfirmButton() {
  let result: any = await requestAddSubmenuToAMenuOrUpdateAMenu(ParamsForAddSubMenuOrUpdateMenu)
  console.log(result) // {code: 200, message: '成功', data: null, ok: true}
  if (result.code == 200) {
    isTheDialogVisible.value = false
    ElMessage({
      type: 'success',
      message: ParamsForAddSubMenuOrUpdateMenu.id ? '更新成功' : '添加成功',
    })
    getFlagOfAllOfMenuAndButtonPermission() // 重新获取所有菜单和按钮的权限标识信息，以便更新页面上的数据
  } else {
    ElMessage({
      type: 'error',
      message: ParamsForAddSubMenuOrUpdateMenu.id ? '更新失败' : '添加失败',
    })
  }
}
/**
 * @description 处理用户点击在气泡确认框组件中“确定”按钮时发生的事件
 */
async function handlerForUserClickTheConfirmButtonInThePopConfirmComponent(row: typeOfObjectThatstoreInformationAboutMenuOrButtonPermission) {
  let result = await requestDeleteMenu(row.id)
  console.log(result) // {code: 200, message: '成功', data: null, ok: true}
  if (result.code == 200) {
    isTheDialogVisible.value = false
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getFlagOfAllOfMenuAndButtonPermission()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style scoped></style>